using System;
using System.Windows.Forms;
using Alerter.DataSet;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using E4RPTools;

namespace Alerter.Controls
{
    public partial class GroupXtraForm : XtraForm
    {
        private long _documentId;
        private Controller _controller;
        private GrupoDataSet _dataSet;

        #region Properties

        public long DocumentId
        {
            set { _documentId = value; }
        }

        public Controller Controller
        {
            set { _controller = value; }
        }

        #endregion

        public GroupXtraForm()
        {
            InitializeComponent();
            Load += GroupXtraFormLoad;
            okSimpleButton.Click += OkSimpleButtonClick;
            gridView.InitNewRow += GridViewInitNewRow;
        }        

        #region Private Methods

        private void SetupContactLookupData()
        {
            contactoDataSet.Merge(_controller.GetContacts());
            contactRepositoryItemLookUpEdit.Columns.Clear();
            contactRepositoryItemLookUpEdit.Columns.AddRange(new [] {
                new LookUpColumnInfo("Id", "Id", 81, FormatType.None, "", false, HorzAlignment.Near),
                new LookUpColumnInfo("Nome", "Nome", 85, FormatType.None, "", true, HorzAlignment.Near)});
            contactRepositoryItemLookUpEdit.DataSource = contactoDataSet.Contacto;
            contactRepositoryItemLookUpEdit.DisplayMember = contactoDataSet.Contacto.NomeColumn.ColumnName;
            contactRepositoryItemLookUpEdit.ValueMember = contactoDataSet.Contacto.IdColumn.ColumnName;
        }

        private void MergeData()
        {
            _dataSet = new GrupoDataSet {EnforceConstraints = false};
            if (_documentId <= 0)
            {
                var contact = _dataSet.Grupo.NewGrupoRow();
                contact.Id = _documentId;
                contact.Nome = "";
                _dataSet.Grupo.AddGrupoRow(contact);
            }
            else
                _dataSet.Merge(_controller.GetGroup(_documentId));
        }

        private void GetDataFromControls()
        {
            _dataSet.Grupo[0].Nome = nameTextEdit.Text;
            if (descriptionTextEdit.Text == "") _dataSet.Grupo[0].SetDescricaoNull(); else _dataSet.Grupo[0].Descricao = descriptionTextEdit.Text;
            gridView.CloseEditor();
            gridView.UpdateCurrentRow();
        }

        private void SetDataToControls()
        {
            nameTextEdit.Text = _dataSet.Grupo[0].Nome;
            gridControl.DataSource = null;
            gridControl.DataMember = "";
            gridControl.DataSource = _dataSet.Contacto;            
        }

        private void ClearControlErrors()
        {
            dxErrorProvider.SetError(nameTextEdit, "");
            dxErrorProvider.SetError(descriptionTextEdit, "");   
        }

        private Control GetControl(string reference)
        {
            switch (reference)
            {
                case "Nome":
                    return nameTextEdit;
                case "Descricao":
                    return descriptionTextEdit;
                default:
                    return null;
            }
        }

        private void SetControlErrors(Result result)
        {
            foreach (var error in result.Errors)
                dxErrorProvider.SetError(GetControl(error.Reference), error.Message);
        }

        #endregion

        void GroupXtraFormLoad(object sender, EventArgs e)
        {
            MergeData();
            SetDataToControls();
            SetupContactLookupData();            
        }

        void GridViewInitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            var contact = (GrupoDataSet.ContactoRow) gridView.GetDataRow(e.RowHandle);
            contact.IdGrupo = _dataSet.Grupo[0].Id;
        }

        void OkSimpleButtonClick(object sender, EventArgs e)
        {
            ClearControlErrors();
            GetDataFromControls();
            Result result = _controller.IsGroupValid(_dataSet);
            if (result.Failed)
            {
                DialogResult = DialogResult.None;
                SetControlErrors(result);
            }
            else
                _controller.SaveGroup(_dataSet);
        }
    }
}